<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Mockery\Exception;

/**
 * Class MidAutumnIntegral
 * @package App
 */
class MidAutumnIntegral extends Model
{
    protected $table = 'mid_autumn_integral';
    public $timestamps = false;

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'c_id');
    }

    const END_YES = 1;
    const END_NO = 2;

    const PRIZE_NUMBER = 30;
    const TIMES = 3;


    /**
     * 进入游戏
     * @param $user_id
     * @return array
     */
    static function GameIndex($user_id)
    {
        /*
    *1.进入游戏
    * 1.0  是否有上次未结束的游戏
    *   1.0.1 返回上次的分数和游戏ID
    * 1.1  是否有游戏次数
    *   1.1.1 返回游戏次数
    *   1.1.2 返回游戏分数
    *   1.1.3 返回抽奖次数
    */

        //抽奖次数
        $luck_times = MidAutumnLog::LuckDrawTimes($user_id);
        //今天的起止时间
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;

        //1.0  是否有上次未结束的游戏
        $game_last = MidAutumnIntegral::select('id', 'number', 'times', 'end_times')
            ->where('user_id', $user_id)
            ->whereBetween('created_at', [$beginToday, $endToday])
            ->first();
        //如果今天的游戏记录存在  返回剩余次数 和分数
        if ($game_last) {
            $game_times = $game_last->times - $game_last->end_times;
            $data = ['number' => $game_last->number, 'luck_times' => $luck_times, 'game_times' => $game_times, 'prize_number' => self::PRIZE_NUMBER];
            return success(compact('data'));
        }
        //如果今天的游戏记录不存在    则说明今天还没有玩
        $data = ['number' => 0, 'luck_times' => $luck_times, 'game_times' => self::TIMES, 'prize_number' => self::PRIZE_NUMBER];
        return success(compact('data'));
    }

    /**
     * 开始游戏
     * @param $user_id
     * @return array
     */
    static function StartGame($user_id)
    {

        /*
         * 开始游戏
         * 1.0 是否有今天未结束的游戏  返回游戏分值和ID
         * 1.1 是否还有游戏次数  返回游戏次数 和当前分值  ID
         * 1.1.1 有游戏次数  次数减一
         */
        //抽奖次数
        $luck_times = MidAutumnLog::LuckDrawTimes($user_id);
        //今天的起止时间
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $game_last = MidAutumnIntegral::select('id', 'number', 'times', 'end_times')
            ->where('user_id', $user_id)
            ->whereBetween('created_at', [$beginToday, $endToday])
            ->first();
        // 是否有今天的游戏数据
        if ($game_last) {
            $game_times = $game_last->times - $game_last->end_times;
            $data = ['number' => $game_last->number, 'luck_times' => $luck_times, 'game_times' => $game_times, 'prize_number' => self::PRIZE_NUMBER];
            return success(compact('data'));
        }
        //没有今天的游戏记录或者还有游戏次数  建立一条数据
        try {
            if (!$id = MidAutumnIntegral::insertGetId([
                'user_id' => $user_id,
                'created_at' => time()
            ])) {
                return error('开始游戏失败');
            }
        } catch (Exception $exception) {
            logger($exception);
            return error('开始游戏失败');
        }
        $data = ['number' => 0, 'game_times' => self::TIMES, 'luck_times' => $luck_times, 'prize_number' => self::PRIZE_NUMBER];
        return success(compact('data'));
    }

    /**
     * 增加积分
     * @param $user_id
     * @return array
     */
    static function AddIntegral($user_id)
    {
        //今天的起止时间
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $game_last = MidAutumnIntegral::select('id', 'times', 'end_times')
            ->where('user_id', $user_id)
            ->whereBetween('created_at', [$beginToday, $endToday])
            ->first();
        if (!$game_last || ($game_last->times - $game_last->end_times) <= 0) {
            return error('你在干什么?');
        }
        try {
            //自增
            if (!MidAutumnIntegral::where('id', $game_last->id)
                ->increment('number')) {
                return error('失败');
            }
            return success(['message' => '成功']);
        } catch (Exception $exception) {
            logger($exception);
            return error('失败');
        }

    }


    /**
     * 结束游戏
     * @param $integral_id
     * @param $number
     * @return array
     */
    static function EndGame($user_id)
    {
        //抽奖次数
        $luck_times = MidAutumnLog::LuckDrawTimes($user_id);
        //今天的起止时间
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $game_last = MidAutumnIntegral::select('id', 'times', 'end_times', 'number')
            ->where('user_id', $user_id)
            ->whereBetween('created_at', [$beginToday, $endToday])
            ->first();
        $game_times = $game_last->times - $game_last->end_times;
        if (!$game_last || ($game_times <= 0)) {
            return error('你在干什么?');
        }
        try {
            //自增
            if (!MidAutumnIntegral::where('id', $game_last->id)
                ->increment('end_times')) {
                return error('失败');
            }
            $data = ['number' => $game_last->number, 'luck_times' => $luck_times, 'game_times' => $game_times - 1, 'prize_number' => self::PRIZE_NUMBER];
            return success(compact('data'));
        } catch (Exception $exception) {
            logger($exception);
            return error('失败');
        }
    }


    /**
     * 抽奖减分
     * @param $user_id
     * @return array
     */
    static function IntegralMul($user_id)
    {
        $id = MidAutumnIntegral::where('user_id', $user_id)
            ->where('number', '>=', self::PRIZE_NUMBER)
            ->value('id');
        try {
            if (!MidAutumnIntegral::where('id', $id)->decrement('number', self::PRIZE_NUMBER)) {
                return error('出错了');
            }
        } catch (Exception $exception) {
            logger($exception);
            return error('出错了');
        }


        return success();
    }

}
